
import { CCBoolean, Component, Enum, EventTouch, find, Node, UITransform, _decorator } from 'cc';
import { DEV } from 'cc/env';
import { ISuperContainerListener } from './ISuperContainerListener';
import { SuperButton } from '../base/SuperButton';
const { ccclass, property, requireComponent } = _decorator;



@ccclass('SuperContainer')
export class SuperContainer extends Component {
    @property(CCBoolean)
    @property({ tooltip: DEV && "是否循环滚动" })
    loop: boolean = false;

    @property({ tooltip: DEV && "该容器从激活状态到非激活状态时是否取消选中。" })
    @property
    exitUnSelected: boolean = true;

    private _listener: ISuperContainerListener = null;
    private _isKeyboardActive: boolean = false;

    protected _hoverItemView: SuperButton = null;
    protected _selectedItemView: SuperButton = null;


    public get hoverItemView() {
        return this._hoverItemView;
    }

    public get selectedItemView() {
        return this._selectedItemView;
    }

    public set selectedItemView(itemView) {
        this._selectedItemView = itemView;
    }

    public set listener(value: ISuperContainerListener) {
        this._listener = value;
    }

    public get listener() {
        return this._listener;
    }

    public set isKeyboardActive(active: boolean) {
        this._isKeyboardActive = active;
    }

    public get isKeyboardActive() {
        return this._isKeyboardActive;
    }

    public setDefaultSelectedItemView(itemView: SuperButton) {

    }

    public getDefaultSelectedItemView(): SuperButton {
        return null;
    }



    public moveUp(): boolean {
        return false;
    }

    public moveDown(): boolean {
        return false;
    }

    public moveLeft(): boolean {
        return false;
    }

    public moveRight(): boolean {
        return false;
    }

    public selectedDefaultItem() {
    }

    public unselectedItemView() {
        if (this.selectedItemView) {
            this.selectedItemView.onUnSelected();
            this.selectedItemView = null;
        }
    }

    public onSwitchUnselectedItemView() {
        if (!this.exitUnSelected) {
            return;
        }
        this.unselectedItemView();
    }

    public getItemViews(): SuperButton[] {
        return [];
    }

    protected _isHoverInited: boolean = false;//当容器第一次悬停某项时，其他项发送unHover事件
    public onItemHoverCallback(itemView: SuperButton, select: boolean) {
        if (select) {
            if (this._hoverItemView == itemView) {
                this._hoverItemView.setHoverState(select);
            } else {
                if (this._hoverItemView) {
                    this._hoverItemView.setHoverState(false);
                    this._hoverItemView = null;
                } else {
                    if (!this._isHoverInited) {
                        this._isHoverInited = true;
                        let itemViews = this.getItemViews();
                        for (let i = 0; i < itemViews.length; i++) {
                            if (itemViews[i] != itemView) {
                                itemViews[i].setHoverState(false);
                            }
                        }
                    }
                }
                this._hoverItemView = itemView;
                itemView.setHoverState(select);
            }
        } else {
            if (this._hoverItemView == itemView) {
                this._hoverItemView.setHoverState(select);
                this._hoverItemView = null;

            }
        }
    }

    protected _isSelectedInited: boolean = false;//当容器第一次选中某项时，其他项发送unSelect事件
    public onItemSelectedCallback(itemView: SuperButton, select: boolean, event?: EventTouch) {
        if (select) {
            if (this._selectedItemView == itemView) {
                this._selectedItemView.setSelectedState(select);
            } else {
                if (event) {
                    this._listener?.onItemTouch(itemView);
                }
                if (this._selectedItemView) {
                    this._selectedItemView.setSelectedState(false);
                    this._selectedItemView = null;
                } else {
                    if (!this._isSelectedInited) {
                        this._isSelectedInited = true;
                        let itemViews = this.getItemViews();
                        for (let i = 0; i < itemViews.length; i++) {
                            if (itemViews[i] != itemView) {
                                itemViews[i].setSelectedState(false);
                            }
                        }
                    }
                }
                this._selectedItemView = itemView;
                itemView.setSelectedState(select);
            }
        } else {
            if (this._selectedItemView == itemView) {
                this._selectedItemView.setSelectedState(select);
                this._selectedItemView = null;

            }
        }
    }


    getContents(): Component[] {
        return []
    }


}


